package  com.NeuglsWorkStudio.UI.Buttons
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.Colors.HSL;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
		
	public class TSimpleButton extends Sprite
	{
		private var _FCaption:String="";
		private var _FAutoWidth:Boolean =true;
		private var _FIsHtml:Boolean=true;
		protected var FCaptionField:TextField;
		private var _FCaptionFormat:TextFormat;
		private var _FBaceColor:int;
		private var _FEnabled:Boolean=true;

		public function get Enabled():Boolean
		{
			return _FEnabled;
		}

		public function set Enabled(value:Boolean):void
		{
			_FEnabled = value;
			EnableChanged(value);
		}

		public function get CaptionFormat():TextFormat
		{
			return _FCaptionFormat;
		}

		public function set CaptionFormat(value:TextFormat):void
		{
			_FCaptionFormat = value;
			FCaptionField.setTextFormat(_FCaptionFormat);
		}

		public function get BaceColor():int
		{
			return _FBaceColor;
		}

		public function set BaceColor(value:int):void
		{
			_FBaceColor = value;
		}

		public function get OnClick():Function
		{
			return _FOnClick;
		}

		public function set OnClick(value:Function):void
		{
			_FOnClick = value;
		}

		private var _FOnClick:Function=null;
		
		public function TSimpleButton(ACaption:String="",IsHtml:Boolean=true,BaceColor:int=0x222222)
		{
			_FCaption=ACaption;
			_FIsHtml=IsHtml;
			_FBaceColor=BaceColor;
			
			this.buttonMode=true;
			this.mouseChildren=false;
			
			this.addEventListener(MouseEvent.CLICK,FClick,false,0,true);
			FCaptionField = new TextField();
			
			_FCaptionFormat=new TextFormat("Tahoma",12,ColorUtility.GetColorFromBaseColor(_FBaceColor));
			
			FCaptionField.defaultTextFormat=_FCaptionFormat;
			FCaptionField.autoSize=TextFieldAutoSize.LEFT;
			FCaptionField.multiline=true;
			SetCaption();
			this.addChild(FCaptionField);
			FCaptionField.height=this.Height;
			FCaptionField.width=this.Width;
		}
		
		public function get Width():Number
		{
			return FCaptionField.textWidth;
		}


		public function get Height():Number
		{
			return FCaptionField.textHeight;
		}

		public function get Caption():String
		{
			return _FCaption;
		}

		public function set Caption(value:String):void
		{
			_FCaption = value;
			FCaptionField.text = _FCaption;
		}

		protected function FClick(Event:MouseEvent):void{
			if(_FOnClick!=null){
				_FOnClick();
			}
			
		}
		
		protected function SetCaption():void{
			if(_FCaption!=""){
				if(_FIsHtml){
					FCaptionField.htmlText=_FCaption;
				}else{
					FCaptionField.text = _FCaption;
				}
			}
		}
		
		protected function EnableChanged(V:Boolean):void{
			if(V){
				this.addEventListener(MouseEvent.CLICK,FClick,false,0,true);
			}else{
				this.removeEventListener(MouseEvent.CLICK,FClick);
			}
		}
	}	
}